package com.lc.cehui.ui.activity.home;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.request.TechnologyShowApi;
import com.lc.cehui.http.response.TechnologyShowBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.adapter.TabViewPagerAdapter;
import com.lc.cehui.ui.fragment.home.ClassHourFragment;
import com.lc.cehui.ui.fragment.home.TeachFragment;
import com.lc.cehui.ui.fragment.home.TrainingDetailsFragment;
import com.lc.cehui.util.CourseUtils;
import com.lc.cehui.util.SPUtil;
import com.lc.cehui.widget.SlidingTabLayout;
import com.lc.cehui.widget.XCollapsingToolbarLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TrainingDetailsActivity extends AppActivity implements XCollapsingToolbarLayout.OnScrimsListener {
    @BindView(R.id.img_logo)
    ImageView imgLogo;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tv_keshi)
    TextView tvKeshi;
    @BindView(R.id.tv_fuwu)
    TextView tvFuwu;
    @BindView(R.id.tv_dianhua)
    TextView tvDianhua;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.rl_top)
    RelativeLayout rlTop;
    @BindView(R.id.tb_home_title)
    Toolbar tbHomeTitle;
    @BindView(R.id.ctl_home_bar)
    XCollapsingToolbarLayout mCollapsingToolbarLayout;
    @BindView(R.id.slidingTabLayout)
    SlidingTabLayout slidingTabs;
    @BindView(R.id.vp)
    ViewPager vp;
    private TechnologyShowBean data;
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.activity_trainingdetails;
    }

    @Override
    protected void initView() {
        titles.add("详情");
        titles.add("课表");
        titles.add("老师");
        technology_show();

        //设置渐变监听
        mCollapsingToolbarLayout.setOnScrimsListener(this);
    }

    private void technology_show() {
        EasyHttp.post(this)
                .api(new TechnologyShowApi().setTechnology_id(getIntent().getStringExtra("id")))
                .request(new HttpCallback<String>(this) {

                    @Override
                    public void onSucceed(String datas) {
                        SPUtil.remove(TrainingDetailsActivity.this, IntentKey.TEXT);
                        SPUtil.put(TrainingDetailsActivity.this, IntentKey.TEXT, datas);
                        Gson gson = new Gson();
//                将存储的数据转成实体类
                        data = gson.fromJson(datas, new TypeToken<TechnologyShowBean>() {
                        }.getType());

                        title.setText(data.getData().getTitle());
                        tvKeshi.setText(CourseUtils.getMillsToTime(data.getData().getStart_time())+"-"+CourseUtils.getMillsToTime(data.getData().getEnd_time())+"  |  "+data.getData().getClass_hour()+"课时");
                        tvDianhua.setText(data.getData().getTel1()+"        "+data.getData().getTel2());
                        tvAddress.setText(data.getData().getProvince()+data.getData().getCity()+data.getData().getDistrict()+data.getData().getAddress());
                        fragments.add(new TrainingDetailsFragment());
                        fragments.add(new ClassHourFragment());
                        fragments.add(new TeachFragment());
                        vp.setAdapter(new TabViewPagerAdapter(TrainingDetailsActivity.this.getSupportFragmentManager(), TrainingDetailsActivity.this, fragments, titles));
                        slidingTabs.setTabTitleTextSize(13);//标题字体大小
                        slidingTabs.setTitleTextColor(Color.parseColor("#028BFF"), Color.parseColor("#999999"));//标题字体颜色
                        slidingTabs.setTabStripWidth(60);//滑动条宽度
                        slidingTabs.setSelectedIndicatorColors(Color.parseColor("#028BFF"));//滑动条颜色
                        slidingTabs.setDistributeEvenly(true); //均匀平铺选项卡
                        slidingTabs.setViewPager(vp);//最后调用此方法
                        vp.setOffscreenPageLimit(fragments.size());//fragment保存的个数
                        vp.setCurrentItem(0);//设置卡在页面

                    }
                });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.img_back)
    public void onClick() {
        finish();
    }

    @Override
    public void onScrimsStateChange(XCollapsingToolbarLayout layout, boolean shown) {
        if (shown) {
            tbHomeTitle.setBackgroundResource(R.color.main_color);
//            tbHomeTitle.setBackgroundColor(Color.BLACK);
//            getStatusBarConfig().statusBarDarkFont(true).init();
        } else {
            tbHomeTitle.setBackgroundResource(R.color.black100);

//            getStatusBarConfig().statusBarDarkFont(false).init();
        }
    }
}
